import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/community_business/post_detail_page/community_image_page.dart';
import 'package:xvideos/hj_utils/api_service/common_service.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/hj_utils/history_record_util.dart';

import '../../alert_business/video/video_buy_bottom_sheet.dart';

class AcgGameDetailLogic extends GetxController with GetTickerProviderStateMixin {
  final String gameId;
  AcgGameDetailLogic({this.gameId = ''});

  final List<String> tabs = ['相关推荐', '评论'];
  late TabController tabCtr = TabController(length: tabs.length, vsync: this);
  VideoModel? detailInfo;
  Rx<int> totalCtCount = 0.obs;

  @override
  void onReady() {
    super.onReady();
    loadData();
  }

  loadData() async {
    detailInfo = await VidService.getVideoDetail(gameId);
    if (detailInfo != null) {
      totalCtCount.value = detailInfo?.commentCount ?? 0;
      HistoryRecordUtil.insertHistoryModel(
        detailInfo,
        HistoryRecordType.game,
      );
    }
    detailInfo ??= VideoModel();
    update();
  }

  ////金币解锁
  showAlert() async {
    if (detailInfo?.gameLockType == 1) {
      var ret = await Get.bottomSheet(VideoBuyBottomSheet(videoM: detailInfo!));
      if (ret == true) {
        detailInfo?.vidStatus?.hasPaid = true;
        update(['link']);
        Clipboard.setData(ClipboardData(text: detailInfo?.seedLinkUrl ?? ""));
        showToast("链接地址复制成功");
      }
    }
  }

  onLinkClickAction() {
    Clipboard.setData(ClipboardData(text: detailInfo?.seedLinkUrl ?? ""));
    showToast("链接地址复制成功");
  }

  onShowImgs(int index) {
    Get.to(CommunityImagePage(
      imageList: detailInfo?.seriesCover,
      index: index,
    ));
  }

  //收藏
  onCollectionAction() async {
    String type = 'SEED_LINK';
    String? objID = detailInfo?.id;
    bool hasCollected = detailInfo?.vidStatus?.hasCollected ?? false;

    if (hasCollected) {
      final success = await MineService.postCollect(objID, type, false);
      if (success) {
        detailInfo?.collectCount = (detailInfo?.collectCount ?? 1) - 1;
        detailInfo?.vidStatus?.hasCollected = false;
        detailInfo?.collectCount = max(0, detailInfo?.collectCount ?? 0);
        showToast("已取消收藏");
      }
    } else {
      final success = await MineService.postCollect(objID, type, true);
      if (success) {
        detailInfo?.collectCount = (detailInfo?.collectCount ?? 0) + 1;
        detailInfo?.vidStatus?.hasCollected = true;
        detailInfo?.collectCount = max(0, detailInfo?.collectCount ?? 0);
        showToast("收藏成功");
      }
    }
    update(['collect']);
  }

  //点赞
  onLikeAction() async {
    String type = 'SEED_LINK';
    String? objID = detailInfo?.id;
    bool hasLiked = detailInfo?.vidStatus?.hasLiked ?? false;

    if (hasLiked) {
      await CommonService.cancelLike(objID, type);
      detailInfo?.likeCount = (detailInfo?.likeCount ?? 1) - 1;
      detailInfo?.likeCount = max(0, detailInfo?.likeCount ?? 0);
      detailInfo?.vidStatus?.hasLiked = false;
      showToast("取消点赞");
    } else {
      await CommonService.sendLike(objID, type);
      detailInfo?.likeCount = (detailInfo?.likeCount ?? 0) + 1;
      detailInfo?.likeCount = max(0, detailInfo?.likeCount ?? 0);
      detailInfo?.vidStatus?.hasLiked = true;
      showToast("点赞成功");
    }
    update(['like']);
  }
}
